package com.faxsun.payment.service.gateway;

import javax.annotation.Resource;

import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.order.service.OrderService;

import com.faxsun.api.service.FaxsunService;
import com.faxsun.core.order.service.FSOrderService;
import com.faxsun.model.response.Response;
import com.faxsun.model.response.result.QueryPaymentResult;

public class PaySSSHostedGatewayServiceImpl implements PaySSSHostedGatewayService {

	@Resource(name = "blOrderService")
	protected OrderService orderService;

	@Override
	public boolean isEnabledSignVerify() {
		return true;
	}

	@Override
	public String callPaymentResult(final String orderId, final int orderAmount, final String itemTitle,
			final String itemDesc) {
		return FaxsunService.createPayment(orderId, orderAmount, itemTitle, itemDesc);
	}

	@Override
	public Response<QueryPaymentResult> queryPaymentResultByOrderId(long orderId) {
		FSOrderService fsOrderService = null;
		if (orderService instanceof FSOrderService) {
			fsOrderService = (FSOrderService) orderService;
		}

		Order order = null;
		if (fsOrderService != null) {
			order = orderService.findOrderById(orderId);
			if (order == null) {
				return null;
			}

			String sellerOrderId = fsOrderService.getSellOrderId(order);
			if (sellerOrderId == null) {
				return null;
			}

			return FaxsunService.queryPayment(null, sellerOrderId);
		} else {
			return null;
		}
	}

}
